﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;

namespace SpielMitMirFinal.Models
{
    public class Human : Player
    {

        public Human()
        {
            oponentsMap = new Map();
            myMap = new Map();
        }

        #region Ovverride

        public override void UpdateImpression(Point point, bool hit)
        {
            if (hit)
            {
                oponentsMap.board[point.X, point.Y] = 2;
            }
            else
            {
                oponentsMap.board[point.X, point.Y] = -1;
            }
        }

        public override bool CheckFiringPosition(Point point)
        {
            if (myMap.board[point.X, point.Y] == 1)
            {
                myMap.board[point.X, point.Y] = 2;
                CheckIfDead(point);
                return true;
            }
            else
            {
                myMap.board[point.X, point.Y] = -1;
                return false;
            }
        }

        public override void CheckIfDead(Point point)
        {
            IGShip shipToBeRemoved = new IGShip();
            foreach (IGShip ship in myShips)
            {
                foreach (Point shipPoint in ship.location)
                {
                    if (shipPoint == point)
                    {
                        bool dead = true;
                        foreach (Point otherPartsOfSameShip in ship.location)
                        {
                            if (myMap.board[otherPartsOfSameShip.X, otherPartsOfSameShip.Y] != 2)
                            {
                                dead = false;
                            }
                        }
                        if (dead)
                        {
                            shipToBeRemoved = ship;
                            break;
                        }
                        break;
                    }
                }
            }
            myShips.Remove(shipToBeRemoved);
        }

        public override void DrawShips()
        {
            foreach (IGShip ship in myShips)
            {
                foreach (Point point in ship.location)
                {
                    myMap.board[point.X, point.Y] = 1;
                }
            }
        }

        #endregion

        public void PlaceShip(List<Point> location)
        {
            IGShip sh = new IGShip();
            sh.location = location;
            myShips.Add(sh);
            DrawShips();
        }


       

    }
}
